#include "settingpage.h"
#include "ui_settingpage.h"
#include "Config/constant.h"
#include "QPainter"
SettingPage::SettingPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SettingPage)
{
    ui->setupUi(this);
//    setBg();
    if(Constant::ShowFullScreen)
        this->showFullScreen();
    initialCombox();
    setting=new QSettings("./Configuration.ini",QSettings::IniFormat);
    getConfigValue();

}

SettingPage::~SettingPage()
{
    delete setting;
    delete ui;
}
void SettingPage::getConfigValue()
{
    setting->beginGroup("Config");
    QString comName=setting->value("COM").toString();
    QString timeName=setting->value("Time").toString();
    QString printType=setting->value("PrintType").toString();
    QString jgName=setting->value("User").toString();
    QString reportName=setting->value("ReportName").toString();
    setting->endGroup();
    int comNum=ui->comboBox_SerialPort->count();
    if(comNum>0)
    {
        for(int i=0;i<comNum;i++)
        {
            if(ui->comboBox_SerialPort->itemText(i)==comName)
            {
                ui->comboBox_SerialPort->setCurrentText(comName);
            }
        }
    }else
    {
        ui->comboBox_SerialPort->clear();
    }
    if(printType.length()>0)
        ui->comboBox_PrintType->setCurrentText(printType);


    int TimeNum=ui->comboBox_Time->count();
    if(TimeNum>0)
    {
        for(int i=0;i<TimeNum;i++)
        {
            if(ui->comboBox_Time->itemText(i)==timeName)
            {
                ui->comboBox_Time->setCurrentText(timeName);
            }
        }
    }else
    {
        ui->comboBox_Time->clear();
    }
    ui->lineEdit_JG->setText(jgName);
    ui->lineEdit_RePortName->setText(reportName);


}
void SettingPage::initialCombox()
{
    ui->comboBox_Time->clear();
    ui->comboBox_Time->addItem("3分钟");
    ui->comboBox_Time->addItem("5分钟");
    serialportsearch();
}
void SettingPage::on_bt_return_clicked()
{
    this->close();
}


void SettingPage::on_pb_Save_clicked()
{
    setting->beginGroup("Config");
    setting->setValue("COM",ui->comboBox_SerialPort->currentText());
    setting->setValue("Time",ui->comboBox_Time->currentText());
    setting->setValue("User",ui->lineEdit_JG->text());
    setting->setValue("PrintType",ui->comboBox_PrintType->currentText());
    setting->setValue("ReportName",ui->lineEdit_RePortName->text());
    setting->endGroup();
    this->close();

}

void SettingPage::serialportsearch()
{
    ui->comboBox_SerialPort->clear();
    //通过QSerialPortInfo查找可用串口
    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        qDebug()<<info.portName();
        qDebug()<<info.vendorIdentifier();
        qDebug()<<info.productIdentifier();
        if(info.vendorIdentifier()==0x0403 && info.productIdentifier()==0x6001)
        {
            ui->comboBox_SerialPort->addItem(info.portName());
        }

    }
}
/**
 * 功能：设置背景图片
 * @brief SettingPage::setBg
 */
void SettingPage::setBg()
{
    QPalette pal = this->palette();
    pal.setBrush(QPalette::Window,QBrush(QPixmap(":/images/MeasurePage/measure_bg.png")));
    setPalette(pal);
}
void SettingPage::paintEvent(QPaintEvent *event)
{
    //通过这种方式可以将图片平铺到窗口上
    //使用样式表可以很方便设置界面，而且非常高效，还能让界面和逻辑分离
    //不过在本例中由于是顶层窗口，所以直接设置样式表也不会显示。
    //所以样式表比较适合子窗口来使用。
    QPainter painter(this);
    painter.drawPixmap(rect(),QPixmap(":/images/MeasurePage/measure_bg.png"),QRect());
}
